Home > Java Programming > Inner Classes > Questions and Answers
01. |
What is the output for the below code ? public class Outer { private int a = 7; class Inner { public void displayValue() { System.out.println("Value of a is " + a); } } } public class Test { public static void main(String... args) throws Exception { Outer mo = new Outer(); Outer.Inner inner = mo.new Inner(); inner.displayValue(); } } | |||||||||||
|
02. |
What is the output for the below code ? public class Tech { public void tech() { System.out.println("Tech"); } } public class Atech { Tech a = new Tech() { public void tech() { System.out.println("anonymous tech"); } }; public void dothis() { a.tech(); } public static void main(String... args){ Atech atech = new Atech(); atech.dothis(); } | |||||||||||
|
03. |
class MyOuter { class MyInner { } } after compile how many classes will be created ? | |||||||||||
|
04. |
What is the output for the below code ? public abstract class A { public void printValue(){ System.out.println("A"); } } 1. public class Test{ 2. public static void main (String[] args){ 3. A a1 = new A() { 4. }; 5. a1.printValue(); 6. } 7. } | |||||||||||
|
05. | Which statement is true? | |||||||||||
|
06. |
What is the output for the below code ? class Outer { private String x = "Outer variable"; void doStuff() { String z = "local variable"; class Inner { public void seeOuter() { System.out.println("Outer x is " + x); System.out.println("Local variable z is " + z); } } } } | |||||||||||
|
07. |
What is the output for the below code ? class Outer { private String x = "Outer variable"; void doStuff() { final String z = "local variable"; class Inner { public void seeOuter() { System.out.println("Outer x is " + x); System.out.println("Local variable z is " + z); } } Inner mi = new Inner(); mi.seeOuter(); } public static void main(String... args){ Outer out = new Outer(); out.doStuff(); } } What is the output? | |||||||||||
|
08. |
What is the output for the below code ? public class Tech { public void tech() { System.out.println("Tech"); } } public class Atech { Tech a = new Tech() { public void nontech () { System.out.println("anonymous nontech tech"); } public void tech() { System.out.println("anonymous tech"); } }; public void dothis() { a.tech(); a.nontech(); } public static void main(String... args){ Atech atech = new Atech(); atech.dothis(); } } | |||||||||||
|